﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FieldDevice.Devices
{
    class DevicePMC43M : Device
    {
        public DevicePMC43M(byte address, string name)
            : base(DeviceCategory.PowerMeter, address, name)
        {
            Initialize();
        }

        public DevicePMC43M(byte address)
            : base(DeviceCategory.PowerMeter, address)
        {
            Initialize();
        }

        public const ushort Register_Va = 0;
        public const ushort Register_Vb = 2;
        public const ushort Register_Vc = 4;
        public const ushort Register_Vab = 8;
        public const ushort Register_Vbc = 10;
        public const ushort Register_Vca = 12;
        public const ushort Register_Ia = 16;
        public const ushort Register_Ib = 18;
        public const ushort Register_Ic = 20;
        public const ushort Register_P = 30;
        public const ushort Register_Q = 38;
        public const ushort Register_S = 46;
        public const ushort Register_PF = 51;
        public const ushort Register_EPI = 102;         //有功电度的寄存器地址占两个字节
        public const ushort Register_EQL = 106;

        byte[] RegistersValue = new byte[522];
        public override void DebugReads()
        {
            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
                this.UpdateReads();
            else
                base.DebugReads();
        }
        void UpdateReads()
        {
            ReadRegValue(Emulation.ACR120ELEmulator.ADDR_DIO, 25);
            base.FireReadChangeEvent("ALL", 0.0f);
        }

        public void ReadRegValue(ushort DataAddr, ushort DataNumber)
        {
            RegistersValue = base.ReadRegister(DataAddr, DataNumber);
            FieldLib.ModBusPacket packet = new FieldLib.ModBusPacket(this.ModbusAddress, this.ModBusReadCommand,
                DataAddr, DataNumber);
            TranslateRegister(packet, RegistersValue);
        }

        void Initialize()
        {
            base.RequestRegister(Register_Va, 24);
            base.RequestRegister(Register_P, 24);
            base.RequestRegister(Register_EPI, 10);
        }

        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            uint tmpValue = FieldLib.Utility.BytesToUInt(value[begin + 3], value[begin + 2], value[begin + 1], value[begin]);
            switch (registeraddress)
            {
                case Register_Va:
                    RecordRead("VA", tmpValue / 100.0f);
                    break;

                case Register_Vb:
                    RecordRead("VB", tmpValue / 100.0f);
                    break;

                case Register_Vc:
                    RecordRead("VC", tmpValue / 100.0f);
                    break;

                case Register_Vab:
                    RecordRead("Vab", tmpValue / 100.0f);
                    break;
                case Register_Vbc:
                    RecordRead("Vbc", tmpValue / 100.0f);
                    break;

                case Register_Vca:
                    RecordRead("Vca", tmpValue / 100.0f);
                    break;

                case Register_Ia:
                    RecordRead("IA", tmpValue / 1000.0f);
                    break;

                case Register_Ib:
                    RecordRead("IB", tmpValue / 1000.0f);
                    break;

                case Register_Ic:
                    RecordRead("IC", tmpValue / 1000.0f);
                    break;

                case Register_PF:
                    RecordRead("PF", tmpValue / 1000.0f);
                    break;
                case Register_P: ;
                    RecordRead("P", tmpValue / 1000.0f);
                    break;
                case Register_Q: ;
                    RecordRead("Q", tmpValue / 1000.0f);
                    break;

                case Register_S: ;
                    RecordRead("S", tmpValue / 1000.0f);
                    break;

                case Register_EPI:
                    RecordRead("EPI", tmpValue);
                    break;

                case Register_EQL:
                    RecordRead("EQL", tmpValue);
                    break;
                default:
                    break;

            }
            return 0.0f;
        }

    }
     
}
